# LuckPay Web Manage System
#
# Copyright (c) 2016 Lucky Byte, Inc.
#

update_batch_button_status = ->
    if page_perms.check
        $('button#batch_check').removeClass('has_clicked').text('批量复核')
    if page_perms.disable
        $('button#batch_disable').removeClass('has_clicked').text('批量禁用')
        $('button#batch_enable').removeClass('has_clicked').text('批量启用')
    if page_perms.delete
        $('button#batch_delete').removeClass('has_clicked').text('批量删除')

    checked_boxes = $('tbody .checkbox.checked#batch')
    if checked_boxes.length == 0
        $('button#batch_check').addClass('disabled')
        $('button#batch_disable').addClass('disabled')
        $('button#batch_enable').addClass('disabled')
        $('button#batch_delete').addClass('disabled')
        return

    batch_editable = true
    batch_checkable = true
    batch_disable = true
    batch_enable = true
    batch_deletable = true
    batch_deletable_ex = true

    # 判断当前的选择是否可以执行批修改操作
    if page_perms.batch and page_perms.edit
        for box in checked_boxes
            if not $(box).data('batch-editable')
                batch_editable = false
                break
    else
        batch_editable = false

    # 判断当前的选择是否可以执行批复核操作
    if page_perms.batch and page_perms.check
        for box in checked_boxes
            if not $(box).data('batch-checkable')
                batch_checkable = false
                break
    else
        batch_checkable = false

    # 判断当前的选择是否可以执行批禁用操作
    if page_perms.batch and page_perms.disable
        for box in checked_boxes
            if not $(box).data('batch-disable')
                batch_disable = false
                break
    else
        batch_disable = false

    # 判断当前的选择是否可以执行批启用操作
    if page_perms.batch and page_perms.disable
        for box in checked_boxes
            if not $(box).data('batch-enable')
                batch_enable = false
                break
    else
        batch_enable = false

    # 判断当前的选择是否可以执行批删除操作
    if page_perms.batch and page_perms.delete
        for box in checked_boxes
            if not $(box).data('batch-deletable')
                batch_deletable = false
                break
    else
        batch_deletable = false

    # 判断当前的选择是否可以执行批删除操作(ex)
    if page_perms.batch and page_perms.delete
        for box in checked_boxes
            if not $(box).data('batch-deletable-ex')
                batch_deletable_ex = false
                break
    else
        batch_deletable_ex = false

    if batch_checkable
        $('button#batch_check').removeClass('disabled')
    else
        $('button#batch_check').addClass('disabled')
    if batch_disable
        $('button#batch_disable').removeClass('disabled')
    else
        $('button#batch_disable').addClass('disabled')
    if batch_enable
        $('button#batch_enable').removeClass('disabled')
    else
        $('button#batch_enable').addClass('disabled')
    if batch_deletable != batch_deletable_ex
        $('button#batch_delete').removeClass('disabled')
    else
        $('button#batch_delete').addClass('disabled')

$ ->
    $('button#new').click -> window.location = '/merch/new'
    $('button#import').click -> window.location = '/merch/import'
    $('button#export').click -> window.location = '/merch/export'

    # 转到清算商户页面
    $('i#settl').click ->
        uuid = $(this).data('uuid')
        window.location = "/merch/settl?settl_merid=#{uuid}"

    # 转到修改页面
    $('i#input').click ->
        uuid = $(this).data('uuid')
        window.location = "/merch/input/#{uuid}"

    # 转到复核界面
    $('i#check').click ->
        uuid = $(this).data('uuid')
        action = $(this).data('action')
        window.location = "/merch/check/#{action}/#{uuid}"

    # 批量处理按钮事件
    $('button#batch').click ->
        $('th#status, td#status').hide()
        $('th#batch, td#batch').show()
        $('form#search_form').find('.input').addClass('disabled')
        $('tfoot').find('*').addClass('disabled').attr('disabled', 'disabled')
        $('.buttons#primary').hide()
        $('.buttons#secondary').show('fast')

    # 退出批量处理按钮事件
    $('button#batch_exit').click ->
        $('th#batch, td#batch').hide()
        $('th#status, td#status').show()
        $('form#search_form').find('.input').removeClass('disabled')
        $('tfoot').find('*').removeClass('disabled').removeAttr('disabled')
        $('.buttons#secondary').hide()
        $('.buttons#primary').show('fast')

    # 批量处理模式下，选择框改变时处理
    $('.checkbox#batch').each ->
        $(this).checkbox
            onChecked:   -> update_batch_button_status()
            onUnchecked: -> update_batch_button_status()

    # 重置所有批量处理按钮为默认状态
    reset_batch_buttons = ->
        $('button#batch_check').removeClass('has_clicked').text('批量复核')
        $('button#batch_disable').removeClass('has_clicked').text('批量禁用')
        $('button#batch_enable').removeClass('has_clicked').text('批量启用')
        $('button#batch_delete').removeClass('has_clicked').text('批量删除')

    # 批量复核
    $('button#batch_check').click ->
        if $(this).hasClass('has_clicked')
            $(this).removeClass('has_clicked').text('批量复核')
            records = []
            $('tbody .checkbox.checked#batch').each ->
                records.push
                    serial: $(this).data('serial')
                    uuid: $(this).data('uuid')
            $(this).addClass 'loading'
            $.post '/merch/batch/check', {
                records: JSON.stringify(records)
            }, (resp) ->
                $('button#batch_check').removeClass 'loading'
        else
            reset_batch_buttons()
            $(this).addClass('has_clicked').text('确认批量复核')

    # 批量禁用
    $('button#batch_disable').click ->
        if $(this).hasClass('has_clicked')
            $(this).removeClass('has_clicked').text('批量禁用')
            records = []
            $('tbody .checkbox.checked#batch').each ->
                records.push
                    serial: $(this).data('serial')
                    uuid: $(this).data('uuid')
            $(this).addClass 'loading'
            $.post '/merch/batch/disable', {
                records: JSON.stringify(records)
            }, (resp) ->
                $('button#batch_disable').removeClass 'loading'
        else
            reset_batch_buttons()
            $(this).addClass('has_clicked').text('确认批量禁用')

    # 批量启用
    $('button#batch_enable').click ->
        if $(this).hasClass('has_clicked')
            $(this).removeClass('has_clicked').text('批量启用')
            records = []
            $('tbody .checkbox.checked#batch').each ->
                records.push
                    serial: $(this).data('serial')
                    uuid: $(this).data('uuid')
            $(this).addClass 'loading'
            $.post '/merch/batch/enable', {
                records: JSON.stringify(records)
            }, (resp) ->
                $('button#batch_enable').removeClass 'loading'
        else
            reset_batch_buttons()
            $(this).addClass('has_clicked').text('确认批量启用')

    # 批量删除
    $('button#batch_delete').click ->
        if $(this).hasClass('has_clicked')
            $(this).removeClass('has_clicked').text('批量删除')
            records = []
            deleteable_ex = false
            $('tbody .checkbox.checked#batch').each ->
                deleteable_ex = $(this).data('batch-deletable-ex')
                records.push
                    serial: $(this).data('serial')
                    uuid: $(this).data('uuid')
            $(this).addClass 'loading'
            url = '/merch/batch/delete'
            if deleteable_ex then url += "_ex"
            $.post url, {
                records: JSON.stringify(records)
            }, (resp) ->
                $('button#batch_delete').removeClass 'loading'
        else
            reset_batch_buttons()
            $(this).addClass('has_clicked').text('确认批量删除')
